﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace Common.DataAnnotations
{
    /// <summary>
    /// 登录密码验证（6-20位）
    /// </summary>
    public class LoginPasswordAttribute : ValidationAttribute
    {
        /// <summary>
        /// 
        /// </summary>
        public LoginPasswordAttribute()
        {

        }

        /// <summary>
        /// 是否为空
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>

        public override bool IsValid(object value)
        {
            if (value == null)
            {
                return true;
            }
            var temp = value.ToString();
            if (temp.Length < 6)
            {
                if (string.IsNullOrEmpty(ErrorMessage))
                    ErrorMessage = "{0}至少6位数字或字母！";
                return false;
            }
            if (temp.Length > 20)
            {
                if (string.IsNullOrEmpty(ErrorMessage))
                    ErrorMessage = "{0}不能超过20位数字或字母！";
                return false;
            }
            //if (!Regex.IsMatch(temp, @"^\w+$"))
            //{
            //    ErrorMessage = "{0}必须是数字或字母，不能出现其他符号！";
            //    return false;
            //}
            return true;
        }
    }
}
